<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/7/19
 * Time: 9:30
 */

namespace app\ext\service;

use app\ext\model\ArticleModel;
use app\ext\model\CateModel;
use app\ext\model\UserModel;

class ArticleService extends BaseService
{
    public function articleList($where = [], $page = 1,$limit = 10)
    {
        $model = new ArticleModel();
        $start = ($page-1)*$limit;
        $articlelist = $model->articleList($where, $start,$limit);

        foreach($articlelist as &$value){
            $strend = strpos($value['content'], '</p>') + 4;
            $value['content'] = substr($value['content'], 0, $strend);
        }

        return $articlelist;
    }

    public function detail($id = 0)
    {
        $articleModel = new ArticleModel();
        $article = $articleModel->articleDetail($id);

        $cateModel = new CateModel();
        $cateInfo = $cateModel->cateInfo($article['c_id']);
        $article['cate'] = $cateInfo['name'];

        $userModel = new UserModel;
        $article['user'] = $userModel->userInfo($article['u_id']);

        return $article;
    }

    public function articleSave()
    {
        $title = input('title');
        $content = input('content');
        if(empty($content))return ['status'=>false,'msg'=>'发布内容不能为空'];

        $data = [
            'title' =>  $title,
            'content'   =>  $content,
            'add_time'  =>  time(),
            'c_id'      =>  0
        ];
        $articleModel = new ArticleModel();
        $res = $articleModel->articleSave($data);
        return ['status'=>true,'msg'=>$res];
    }

    public function del()
    {
        $id = input('id');

        $articleModel = new ArticleModel();
        return $articleModel->del($id);

    }
}